import React from 'react';
import classNames from 'classnames';
import { message, Button, Popconfirm, Tooltip } from 'antd';
import If from '@/components/If';
import BizIcon from '@/components/BizIcon';
import { isSuccess } from '@/utils/request';
import helper from '@/utils/auth/helper';
import { premiumBaseService } from '@/services/salaryManage/salaryCalculate';

import { amountColumn } from '@/pages/SalaryManage/utils/TableUtil';
import { goToPremiumBaseLog } from '@/pages/SalaryManage/utils/pageNavigateUtil';
import ExcelListBaseModel from '@/pages/SalaryManage/SalaryCalculate/ImportExcel/ExcelListBaseModel';
import AutoDivider from '@/pages/SalaryManage/components/AutoDivider';
import salaryStyle from '@/pages/SalaryManage/salaryManage.less';

const { querySocial, socialDownload, deleteSocial, doCancelSocial, doCompleteSocial } = premiumBaseService;

const namespace = 'premiumBaseModel';
const PremiumBaseModel = ExcelListBaseModel({
    namespace,
    queryService: querySocial,
    initService: null
});

PremiumBaseModel.state.goToLog = goToPremiumBaseLog;
PremiumBaseModel.state.columns = (pagination, { onEdit, onDelete }, status = 0) => [
    {
        title: '序号',
        dataIndex: 'sn',
        render(text, record, index) {
            return !record.$$isSummary ? `${(pagination.current - 1) * pagination.pageSize + index + 1}` : '';
        },
        align: 'center',
        width: 80,
        fixed: 'left'
    },
    {
        title: '姓名',
        dataIndex: 'employeeName',
        width: 132,
        fixed: 'left',
        render(text, record /* , index */) {
            return !record.$$isSummary ? text : '合计';
        }
    },
    {
        title: '工号',
        dataIndex: 'employeeNo'
    },
    {
        title: '社保城市',
        dataIndex: 'socialCityName'
    },
    {
        title: '户籍类型',
        dataIndex: 'housePlaceName'
    },
    amountColumn({
        title: '初始月薪',
        dataIndex: 'encryptBaseSalaryAmt'
    }),
    amountColumn({
        title: '初始月绩效',
        dataIndex: 'encryptKpiMonthAmt'
    }),
    {
        title: '险金基数（个人）',
        align: 'center',
        children: [
            amountColumn(
                {
                    title: '养老保险',
                    dataIndex: 'encryptOldBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '失业保险',
                    dataIndex: 'encryptUnemployBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '医疗保险',
                    dataIndex: 'encryptHospitalBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '生育保险',
                    dataIndex: 'encryptBirthBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '工伤保险',
                    dataIndex: 'encryptHarmBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '大病保险',
                    dataIndex: 'encryptIllBaseAmt'
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '公积金',
                    dataIndex: 'encryptFundBaseAmt'
                },
                { color: true }
            )
        ]
    },
    {
        title: '操作',
        dataIndex: 'v_operate',
        fixed: 'right',
        width: 100,
        render(text, record) {
            return (
                <>
                    {!record.$$isSummary ? (
                        <>
                            <AutoDivider>
                                <If
                                    condition={
                                        helper.checkCode('finance:salary:computation:import:socialinit:edit')
                                    }
                                >
                                    <Tooltip title="编辑">
                                        <Button
                                            className={classNames(salaryStyle.operateIcon)}
                                            onClick={() => onEdit(record)}
                                            disabled={status}
                                        >
                                            <BizIcon type="listfunction_edit_icon_default" />
                                        </Button>
                                    </Tooltip>
                                </If>
                                <If
                                    condition={
                                        helper.checkCode('finance:salary:computation:import:socialinit:del')
                                    }
                                >
                                    <Popconfirm
                                        title={`是否确定删除【${record.employeeName}】的险金基数初始化信息`}
                                        placement="bottomRight"
                                        onConfirm={() => onDelete(record)}
                                        disabled={status}
                                    >
                                        <Tooltip title="删除" placement="top">
                                            <Button className={classNames(salaryStyle.operateIcon)} disabled={status}>
                                                <BizIcon type="DeleteIcon" />
                                            </Button>
                                        </Tooltip>
                                    </Popconfirm>
                                </If>
                            </AutoDivider>
                        </>
                    ) : null}
                </>
            );
        }
    }
];

PremiumBaseModel.effects.init = function* init(state, { put, select }) {
    if (state?.payload?.needReset) {
        yield put({
            type: 'setMultiStore',
            payload: {
                searchModel: {
                    employeeCode: null,
                    period: null
                }
            }
        });
    }

    if (state?.payload?.organizationCode) {
        const searchModel = yield select(s => s[namespace].searchModel);
        yield put({
            type: 'setMultiStore',
            payload: {
                searchModel: {
                    ...searchModel,
                    organizationCode: state?.payload?.organizationCode
                }
            }
        });
    }

    // 查询
    yield put({ type: 'search' });
};

PremiumBaseModel.effects.downloadTemplate = function* downloadExcel({ payload }, { call }) {
    return yield call(socialDownload, payload);
};

PremiumBaseModel.effects.deletePremiumBase = function* deletePremiumBase(
    { payload, callback, currentRecord },
    { call, put }
) {
    console.log(payload);
    console.log(currentRecord);
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: true }
    });
    const response = yield call(deleteSocial, { id: payload });
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: false }
    });

    if (isSuccess(response)) {
        message.success(`【${currentRecord.employeeName}】的险金基数初始化删除成功`);
        yield put({ type: 'search' });
    } else {
        message.error(response.msg);
    }

    if (callback) {
        callback(response);
    }
};

// 取消初始化按钮
PremiumBaseModel.effects.cancelPremiumBase = function* cancelPremiumBase(
    { payload: { organizationCode }, callback },
    { call, put }
) {
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: true }
    });
    const response = yield call(doCancelSocial, organizationCode);
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: false }
    });

    if (isSuccess(response)) {
        message.success('取消初始化成功');
        yield put({ type: 'search' });
    } else {
        message.error(response.msg);
    }

    if (callback) {
        callback(response);
    }
};

// 完成初始化按钮
PremiumBaseModel.effects.completePremiumBase = function* completePremiumBase(
    { payload: { organizationCode }, callback }, { call, put }
) {
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: true }
    });
    const response = yield call(doCompleteSocial, organizationCode);
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: false }
    });

    if (isSuccess(response)) {
        message.success('险金基数初始化成功');
        yield put({ type: 'search' });
    } else {
        message.error(response.msg);
    }

    if (callback) {
        callback(response);
    }
};

export default PremiumBaseModel;
